package gpx;

import java.awt.TextArea;
import java.io.*;

import climbingHills.*;
/**
 * Class cares about gpx files convertion.
 * @author Pepan
*/
public class GpxConvertor {
	TextArea ta = new TextArea(); // text area of Dialog, for important messages
	File clFile; // Selected gpx file
	
	/**
	 * Constructor creates instance of GpxConvertor and give him selected file as param
	 * @param file File for convertion
	 */
	public GpxConvertor(File file) {
		this.clFile = file;
	}
	
	@SuppressWarnings("deprecation")
	public void prinfFileContent(){
		BufferedReader in;
		FileReader fr = null;
		FileWriter fw = null;
		BufferedWriter out;
		String line = null;
		myDate md = new myDate();
		TrackPoint tp = null;
		Hill<TrackPoint> h = new Hill<TrackPoint>();
		int i = 0;

		try {
			fr = new FileReader(this.clFile);
			fw = new FileWriter("c:\\_data\\export.gpx");
		} catch (FileNotFoundException e) {
			this.ta.appendText("\nFile was not found");
			e.printStackTrace();
		} catch (IOException e) {
			this.ta.appendText("\nFile was not created");
			e.printStackTrace();
		}
		in = new BufferedReader(fr);
		out = new BufferedWriter(fw);
		
		this.ta.appendText("\nWorking . . . .");
		try {
			while ((line = in.readLine()) != null) {
				out.write(line);
				if (line.endsWith("</ele>")){
					out.write(md.getDateFormat());
				}
				if (line.endsWith("<trk>")){
					out.write("<type>CYCLING_SPORT</type>");
				}
				if (line.startsWith("<trkpt")){
					tp = new TrackPoint(line);
					line = in.readLine();
					if (line.startsWith("  <ele>")) {
						tp.addEle(line);
					}
					h.add(i, tp);
					tp.printTrackPoint(i);
					i++;
				}
				
			
			
			//System.out.println("is");
			
			
			//tp.printTrackPoint();
			//i++;
			}
			
			h.getLast().printTrackPoint(0);

		} catch (IOException e) {
			this.ta.appendText("\nIOException with readline");
			e.printStackTrace();
		}
		
		try {
			
			fr.close();
			
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void saveAs() {
//		JFileChooser fc = new JFileChooser(new File("export.gpx"));
//		fc.addChoosableFileFilter(new MyFilter());
//		fc.showSaveDialog(new Frame());
	}

}
